package com.example.sysao.controller;

import com.example.sysao.pejo.Maintenance;
import com.example.sysao.pejo.Repair;
import com.example.sysao.service.MaintenanceService;
import com.example.sysao.service.ReapirService;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@Component
@RequestMapping("/maintenance")
public class MaintenanceController {
    @Resource
    private MaintenanceService maintenanceService;
    @Resource
    private ReapirService reapirService;

    @RequestMapping("/addarrangment")
    public Map<String, Object> addReminder(Integer id, Integer number) {
        //更改维修状态为3
        System.out.println(id);
        Repair repair = reapirService.findById(id);
        repair.setStatus(2);
        reapirService.saveRepair(repair);
        //新增维修安排
        Maintenance maintenance = new Maintenance();
        LocalDateTime time = LocalDateTime.now();
        maintenance.setRepairId(id);

        maintenance.setTechnicianId(number);
        maintenance.setStartTime(time);

        Map<String, Object> map = new HashMap<>();
        if (maintenanceService.addMaintenance(maintenance)) {
            map.put("success", true);
        }
        return map;
    }

    //取消报修单
//    @RequestMapping("/cancel")
//    public Map<String, Object> cancelRepair(Integer id) {
//        //更改维修状态为
//        System.out.println(id);
//        Repair repair = reapirService.findById(id);
//        repair.setStatus(3);
//        Map<String, Object> map = new HashMap<>();
//        if (reapirService.saveRepair(repair)) {
//            map.put("success", true);
//        }
//        return map;
//    }

    //确认检修
    @PostMapping("/sure")
    public Map<String, Object> surelRepair(@RequestBody Repair repair) {
        //更改维修状态为
        System.out.println(repair);
        repair.setStatus(5);
        Map<String, Object> map = new HashMap<>();
        if (reapirService.saveRepair(repair)) {
            map.put("success", true);
        }
        return map;
    }

    @RequestMapping("/cancel")
    public Map<String, Object> cancelRepair(@RequestBody Repair repair) {
        //更改维修状态为
        repair.setStatus(3);
        Map<String, Object> map = new HashMap<>();
        if (reapirService.saveRepair(repair)) {
            map.put("success", true);
        }
        return map;
    }

    @RequestMapping("list")
    public List<Maintenance> findAll() {
        return maintenanceService.findAll();
    }

    //取消维修安排 更改维修状态
    @RequestMapping("/cancelMaintenance")
    public Map<String, Object> cancelMaintenance(Integer mid, Integer rid) {
        Repair repair = reapirService.findById(rid);
        repair.setStatus(1);
        reapirService.saveRepair(repair);
        Map<String, Object> map = new HashMap<>();
        if (maintenanceService.removeMaintence(mid)) {
            map.put("success", true);
        }
        return map;
    }


}
